<template>
  <el-dialog title="批量新增" :close-on-click-modal="false" :visible.sync="visible" width="50%" @closed="close">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px"
    >
      <el-form-item label="餐台名称前缀" prop="prefix">
        <el-input v-model="dataForm.prefix" placeholder="餐台前缀" class="input"/>
      </el-form-item>
      <el-form-item label="餐台名称序号" prop="nameNum">
        <el-input-number
          class="input"
          v-model="dataForm.startNum"
          placeholder="开始序号"
          step-strictly
          :controls="false"
          :min="0"
          :max="dataForm.endNum || 999999999"
          controls-position="right"
          @change="dataForm.nameNum = dataForm.startNum && dataForm.endNum ? 1 : ''"
        />
        至
        <el-input-number
          class="input"
          v-model="dataForm.endNum"
          placeholder="结束序号"
          step-strictly
          :controls="false"
          :min="(dataForm.startNum || 0) + 1"
          controls-position="right"
          @change="dataForm.nameNum = dataForm.startNum && dataForm.endNum ? 1 : ''"
        />
      </el-form-item>
      <el-form-item label="">
        <el-checkbox-group v-model="checkList">
          <el-checkbox v-for="num in [4, 7, 13]" :key="num" :label="num">不包含数字{{ num }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="餐台所属区域" prop="areaId">
        <el-select v-model="dataForm.areaId" placeholder="区域">
          <el-option v-for="item in areaList" :label="item.areaName" :key="item.areaId" :value="item.areaId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="标准用餐人数" prop="seatNum">
        <el-input-number v-model="dataForm.seatNum" placeholder="请输入1-99的整数" step-strictly :controls="false"
                         :min="1" :max="99" controls-position="right"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
		</span>
  </el-dialog>
</template>

<script>
import { listTableArea } from '@/api/table/tableArea'
import { saveDiningTableBatch } from '@/api/table/diningTable'

export default {
  data() {
    return {
      visible: false,
      loading: false,
      areaList: [],
      dataRule: {
        prefix: [{ required: true, message: '餐台前缀不能为空', trigger: 'blur' }],
        nameNum: [{ required: true, message: '餐台序号不能为空', trigger: 'blur' }],
        areaId: [{ required: true, message: '所属区域不能为空', trigger: 'blur' }],
        seatNum: [{ required: true, message: '所属区域不能为空', trigger: 'blur' }]
      },
      checkList: [],
      dataForm: {
        areaId: undefined,
        startNum: undefined,
        endNum: undefined,
        nameNum: undefined
      }
    }
  },
  mounted() {
    this.listAreas()
  },
  methods: {
    init() {
      this.visible = true
    },
    close() {
      this.$emit('close')
    },
    /**
     * 获取桌台区域
     */
    listAreas() {
      listTableArea().then((response) => {
        this.areaList = response.data
        this.dataForm.areaId = this.areaList[0].areaId
      })
    },
    /**
     * 提交
     */
    dataFormSubmit() {
      if (this.loading) {
        return
      }
      let check = false
      this.$refs['dataForm'].validate(valid => {
        check = valid
      })
      if (!check) {
        return
      }
      this.loading = true
      saveDiningTableBatch({
        ...this.dataForm,
        cantUseNums: this.checkList.length > 0 ? this.checkList.join(',') : ''
      }).then(response => {
        this.$modal.msgSuccess(response.msg)
        this.$emit('refreshDataList')
        this.visible = false
      }).finally(() => this.loading = false)
    }
  }
}
</script>

<style lang="scss" scoped>
.input {
  width: 200px;
}
</style>
